package com.example.mahoutdemo.core.user.video.operation;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mahoutdemo.component.Recommender;
import com.example.mahoutdemo.component.RecommenderConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserVideoOperationService extends ServiceImpl<UserVideoOperationMapper,UserVideoOperation> {

    public List<Long> recommend(Long userId) throws TasteException {
        List<UserVideoOperation> list = list();
        Recommender.SimpleRecommender recommender = Recommender.build(list)
                .userBaseRecommender(RecommenderConstants.SIMILARITY_PEARSON)
                .thresholdUserNeighborhood(0.7)
                .recommender(true);
        //给指定用户推荐10个商品
        List<RecommendedItem> recommend = recommender.recommend(userId, 10);
        //根据上面建立的recommenderBuilder对象和dataModel对象，取数据的70%做测试，预测剩下的30%，查看评测效果
        //评估会很耗时
//        double evaluate = recommender.averageAbsoluteDifferenceEvaluator().evaluate(0.7);
//        log.info("评估结果:{}",evaluate);
        return recommend.stream().map(RecommendedItem::getItemID).collect(Collectors.toList());
    }
}
